package com.xiaoxi.exam.utils;

import cn.xinzhili.xutils.core.util.HashUtils;
import com.xiaoxi.exam.bean.ExamInfoWithTime;
import com.xiaoxi.exam.bean.enums.HasSeat;
import com.xiaoxi.exam.bean.response.ToeflExamListResponse;
import com.xiaoxi.exam.bean.toefl.ExamInfo;
import com.xiaoxi.exam.model.ExamToefl;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;

public class ToeflExamFactory {

  public static ToeflExamListResponse getResponse(List<ExamToefl> exams) {
    ToeflExamListResponse response = new ToeflExamListResponse();
    List<ExamInfoWithTime> result = exams.stream().map(ToeflExamFactory::toInfo)
        .collect(Collectors.groupingBy(ExamInfo::getExamTime)).entrySet().stream().map(e -> {
          ExamInfoWithTime examInfoWithTime = new ExamInfoWithTime();
          examInfoWithTime.setExamTime(e.getKey());
          examInfoWithTime.setExams(e.getValue());
          return examInfoWithTime;
        }).collect(Collectors.toList());
    response.setExamInfos(result);
    return response;
  }

  public static ExamInfo toInfo(ExamToefl exam) {
    ExamInfo examInfo = new ExamInfo();
    BeanUtils.copyProperties(exam, examInfo);
    examInfo.setId(HashUtils.encode(exam.getId()));
    examInfo.setHasSeat(exam.getStatus().equals("名额已报满") ? HasSeat.NONE : HasSeat.HAS);
    return examInfo;
  }

}
